from exit import db
from datetime import datetime
import shortuuid


class Banner(db.Model):     # 轮播
    __tablename__ = 'c_mall_banner'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(255))  # 名字
    image_url = db.Column(db.String(255))  # 图片地址
    link_url = db.Column(db.String(255))  # 跳转地址
    priority = db.Column(db.Integer, default=0)  # 优先级
    create_time = db.Column(db.DateTime, default=datetime.now)  # 时间
    status = db.Column(db.Integer, default=0)
    is_show = db.Column(db.Integer, default=0)  # 展示
    rich_text = db.Column(db.String(255))
    category_id = db.Column(db.Integer)
    goods_id = db.Column(db.Integer)

    def banner_json(self):
        return {
            'id': self.id,
            'name': self.name,
            'image_url': self.image_url,
            'link_url': self.link_url or '无',
            'priority': self.priority,
            'rich_text': self.rich_text,
            'category_id': self.category_id,
            'goods_id': self.goods_id,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
        }


class GoodsStore(db.Model):     # 店铺
    __tablename__ = 'c_mall_goods_store'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    create_time = db.Column(db.DateTime, default=datetime.now)
    name = db.Column(db.String(50))  # 名字
    city = db.Column(db.String(50))     # 城市
    sales = db.Column(db.Integer, default=0)    # 销量
    sales_price = db.Column(db.DECIMAL(20, 2), default=0)  # 销售额
    store_img = db.Column(db.String(255))   # 图片
    priority = db.Column(db.Integer, default=0)  # 优先级
    status = db.Column(db.Integer, default=0)  # 状态
    income = db.Column(db.DECIMAL(20, 2), default=0)  # 余额
    income_s = db.Column(db.DECIMAL(20, 2), default=0)  # 锁定余额
    store_type = db.Column(db.Text)  # 店铺分类
    content = db.Column(db.Text)  # 店铺分类

    def store_json(self):
        return {
            'store_id': self.id,
            'name': self.name,
            'content': self.content,
            'store_img': self.store_img,
        }


class GoodsCategory(db.Model):  # 商品类型
    __tablename__ = 'c_mall_goods_category'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(50))       # 名字
    create_time = db.Column(db.DateTime, default=datetime.now)
    p_path = db.Column(db.Text)     # 上级路径
    p_id = db.Column(db.String(50))     # 上级id
    p_depth = db.Column(db.Integer, default=0)
    status = db.Column(db.Integer, default=0)
    is_show = db.Column(db.Integer, default=0)   # 0展示 1不展示
    is_type = db.Column(db.Integer, default=0)  # 是否转换类型
    priority = db.Column(db.Integer, default=0)  # 优先级
    small_img = db.Column(db.String(255))
    big_img = db.Column(db.String(255))

    def goods_category_json(self):
        return {
            'category_id': self.id,
            'name': self.name,
            'small_img': self.small_img,
            'big_img': self.big_img,
            'priority': self.priority,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
        }


class Goods(db.Model):      # 商品
    __tablename__ = 'c_mall_goods'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    create_time = db.Column(db.DateTime, default=datetime.now)
    price = db.Column(db.DECIMAL(20, 2))    # 价格
    price_high = db.Column(db.DECIMAL(20, 2))    # 价格
    integral = db.Column(db.DECIMAL(20, 2), default=0)  # 积分
    depth = db.Column(db.Integer, default=0)    # 价格深度
    old_price = db.Column(db.DECIMAL(20, 2))   # 历史价格
    freight = db.Column(db.Integer, default=0)     # 运费

    name = db.Column(db.String(255))       # 名字
    goods_img = db.Column(db.String(255))   # 图片
    status = db.Column(db.Integer, default=0)   # 状态
    is_show = db.Column(db.Integer, default=0)  # 展示状态
    is_type = db.Column(db.Integer, default=0)  # 是否转换类型
    rich_text = db.Column(db.String(1024))  # 富文本

    category_id = db.Column(db.Text)     # 类型id
    store_id = db.Column(db.Integer, index=True)   # 商店id

    sales = db.Column(db.Integer, default=0)  # 销量
    # comments = db.Column(db.String(1024))   # 评论
    stock_t = db.Column(db.Integer, default=0)  # 总库存
    stock_c = db.Column(db.Integer, default=0)  # 当前库存
    priority = db.Column(db.Integer, default=0)  # 排序
    goods_code = db.Column(db.String(255))  # 商品编码
    fees = db.Column(db.DECIMAL(20, 2), default=0)     # 手续费
    pay_type = db.Column(db.String(10))     # 支付方式
    user_id = db.Column(db.String(50), index=True)     # 用户id
    token = db.Column(db.String(100), default=shortuuid.uuid)  # token地址
    sku = db.Column(db.String(255))     # sku
    low_price = db.Column(db.DECIMAL(10, 2), default=0)     # 区间低价
    high_price = db.Column(db.DECIMAL(10, 2), default=0)    # 区间高价
    increase_num = db.Column(db.Integer, default=0)     # 交易多少笔上涨
    increase_num_now = db.Column(db.Integer, default=0)     # 交易计数
    increase_price = db.Column(db.DECIMAL(10, 2), default=0)
    cycle = db.Column(db.Integer, default=0)    # 第几轮
    ratio_g = db.Column(db.DECIMAL(5, 2), default=0)    # 供应商价格
    ratio_f = db.Column(db.DECIMAL(5, 2), default=0)    # 服务中心价格
    trade_price = db.Column(db.DECIMAL(20, 2), default=0)    # 服务中心价格
    goods_status = db.Column(db.Integer, default=0)

    def goods_json(self):
        return {
            'goods_id': self.id,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
            'price': self.price,
            'price_pf': self.old_price,
            'name': self.name,
            'stock_c': self.stock_c,
            'stock_t': self.stock_t,
            'sales': self.sales,
            'trade_price': self.trade_price,
            'goods_img': self.goods_img,
            'priority': self.priority,
            'status': self.status,
            'goods_code': self.goods_code,
            'low_price': self.low_price,
            'high_price': self.high_price,
            'fees': self.fees,
        }

    def add_increase_num(self, num):
        self.increase_num_now += num
        self.trade_price += num * self.price
        self.sales += num
        if self.increase_num_now >= self.increase_num:
            self.goods_status = 2


class GoodsDetail(db.Model):    # 商品详情
    __tablename__ = 'c_mall_goods_detail'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    create_time = db.Column(db.DateTime, default=datetime.now)
    name = db.Column(db.String(100))
    goods_id = db.Column(db.Integer, index=True)
    category_id = db.Column(db.Integer, index=True)     # 类型id
    status = db.Column(db.Integer, default=0)   # 0为banner  1为详情
    goods_img = db.Column(db.String(255))  # 图片

    priority = db.Column(db.Integer, default=0)

    def goods_detail_json(self):
        return {
            'goods_id': self.goods_id,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
            'goods_img': self.goods_img,
            'priority': self.priority,
        }


class GoodsSKU(db.Model):   # 商品的SKU
    __tablename__ = 'c_mall_goods_sku'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(100))
    sku_img = db.Column(db.String(256))    # sku图片
    create_time = db.Column(db.DateTime, default=datetime.now)
    category_id = db.Column(db.Integer)  # 商品类型id
    goods_id = db.Column(db.Integer)    # 商品id
    price = db.Column(db.DECIMAL(20, 2))    # 单价
    old_price = db.Column(db.DECIMAL(20, 2))    # 历史价格
    stock_t = db.Column(db.Integer)     # 总库存
    stock_c = db.Column(db.Integer)     # 当前库存
    sales = db.Column(db.Integer, default=0)  # 销量
    is_show = db.Column(db.Integer, default=0)     # 展示
    status = db.Column(db.Integer, default=0)
    priority = db.Column(db.Integer, default=0)  # 排序


class GoodsSpec(db.Model):    # SPU(商品)规格
    __tablename__ = 'c_mall_goods_spec'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    create_time = db.Column(db.DateTime, default=datetime.now)
    goods_id = db.Column(db.Integer)
    name = db.Column(db.String(100))


class SpecOption(db.Model):    # SPU(商品)规格选项
    __tablename__ = 'c_mall_goods_spec_option'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    create_time = db.Column(db.DateTime, default=datetime.now)
    spec_id = db.Column(db.Integer)
    value = db.Column(db.String(100))   # 选项值


class SKUSpec(db.Model):    # SKU(商品详情)规格
    __tablename__ = 'c_mall_goods_sku_spec'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    create_time = db.Column(db.DateTime, default=datetime.now)
    sku_id = db.Column(db.Integer)      # sku id
    spec_id = db.Column(db.Integer)     # 规格id
    option_id = db.Column(db.Integer)   # 选项id


class ShoppingCart(db.Model):    # 购物车
    __tablename__ = 'c_mall_shopping_cart'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    create_time = db.Column(db.DateTime, default=datetime.now)
    user_id = db.Column(db.String(32), index=True)
    goods_id = db.Column(db.Integer, index=True)
    sku_id = db.Column(db.Integer, index=True)
    store_id = db.Column(db.Integer, index=True)    # 商店ID
    status = db.Column(db.Integer, default=0, index=True)
    num = db.Column(db.Integer)

    def shopping_car_json(self):
        return {
            'shopping_id': self.id,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
            'num': self.num,
        }


class BuyDealDeliver(db.Model):
    __tablename__ = 'c_mall_buy_deal_deliver'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user_id = db.Column(db.String(50))
    create_time = db.Column(db.DateTime, default=datetime.now)
    goods_id = db.Column(db.Integer, info='商品id')
    update_time = db.Column(db.DateTime)
    sell_batch_id = db.Column(db.Integer, info='发货批次id')
    num = db.Column(db.Integer, info='发货数量')
    price = db.Column(db.Numeric(10, 2), info='发货商品单价')
    total_price = db.Column(db.Numeric(10, 2), info='发货商品总价')
    logistics_name = db.Column(db.String(32))
    logistics_num = db.Column(db.String(64))
    logistics_simple = db.Column(db.String(64))
    freight = db.Column(db.Integer, info='运费', default=0)
    status = db.Column(db.Integer, default=0)
    sh_name = db.Column(db.String(32))
    sh_phone = db.Column(db.String(32))
    sh_province = db.Column(db.String(255))
    sh_city = db.Column(db.String(255))
    sh_district = db.Column(db.String(255))
    sh_place = db.Column(db.String(255))
    t_type = db.Column(db.Integer, default=0)
    pick_type = db.Column(db.Integer, default=0)

    def buy_deal_deliver_json(self):
        return {
            'id': self.id,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
            'num': self.num,
            'price': self.price,
            'total_price': self.total_price,
            'logistics_name': self.logistics_name,
            'logistics_num': self.logistics_num,
            'logistics_simple': self.logistics_simple,
            'freight': self.freight,
            'status': self.status,
            'sh_name': self.sh_name,
            'sh_phone': self.sh_phone,
            'sh_province': self.sh_province,
            'sh_city': self.sh_city,
            'sh_district': self.sh_district,
            'sh_place': self.sh_place,
            't_type': self.t_type,
            'pick_type': self.pick_type,
        }


class BuyDeal(db.Model):        # 交易订单
    __tablename__ = 'c_mall_buy_deal'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    create_time = db.Column(db.DateTime, default=datetime.now)
    goods_id = db.Column(db.Integer, info='商品id')
    buy_order_id = db.Column(db.String(11), info='挂单买单id')
    sell_order_id = db.Column(db.String(11), info='挂单卖单id')
    buyer_id = db.Column(db.String(50), info='买方')
    seller_id = db.Column(db.String(50), info='卖方')
    sell_batch_id = db.Column(db.String(255), info='卖家售出商品的批次号')
    deal_type = db.Column(db.Integer, info='成交类型 1系统分配，2点对点，3协议转让')
    deal_num = db.Column(db.Integer, info='成交数量')
    deal_price = db.Column(db.DECIMAL(10, 2), info='成交单价，竞拍价')
    deal_money = db.Column(db.DECIMAL(11, 2), info='成交总金额')
    s_num = db.Column(db.Integer, info='该批次剩余数量')
    s_num_sell = db.Column(db.Integer, info='该批次剩余可卖数量')
    tn_day = db.Column(db.Integer, info='t+n')
    t_type = db.Column(db.Integer, default=0)
    buy_fee = db.Column(db.Numeric(20, 2), info='购买手续费')
    sell_fee = db.Column(db.Numeric(20, 2), info='卖出手续费')
    status = db.Column(db.Integer, default=0)

    def buy_deal_json(self):
        return {
            'bath_id': self.id,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
            'deal_type': self.deal_type,
            'deal_num': self.deal_num,
            'deal_price': self.deal_price,
            'deal_money': self.deal_money,
            's_num': self.s_num,
            's_num_sell': self.s_num_sell,
            'tn_day': self.tn_day,
            't_type': self.t_type,
        }


class BuyOrder(db.Model):   # 挂买挂卖订单
    __tablename__ = 'c_mall_buy_order'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user_id = db.Column(db.String(32), index=True)
    create_time = db.Column(db.DateTime, default=datetime.now)
    update_time = db.Column(db.DateTime)
    is_show = db.Column(db.Integer, index=True, default=0)
    status = db.Column(db.Integer, index=True, info='0挂牌中，1已成交，2手动撤销，3系统撤销')
    goods_id = db.Column(db.Integer, index=True)
    num = db.Column(db.Integer, info='数量')
    price = db.Column(db.DECIMAL(20, 2), info='单价')
    total_price = db.Column(db.DECIMAL(20, 2), info='总价')
    surplus_num = db.Column(db.Integer, info='剩余数量')
    surplus_price = db.Column(db.DECIMAL(20, 2), info='剩余冻结金额')
    trade_no = db.Column(db.String(32), unique=True)
    sell_batch_id = db.Column(db.String(50), db.ForeignKey('c_mall_buy_deal.id'), info='卖家售出批次id')
    t_type = db.Column(db.Integer, info='1挂单买入，2挂单卖出')
    reward_pool = db.Column(db.DECIMAL(20, 2), info='奖励额度', default=0)
    scan_time = db.Column(db.DateTime, info='扫描时间')
    scan_status = db.Column(db.Integer, info='扫描状态,0未扫描，1扫描', default=0)
    total_fee = db.Column(db.DECIMAL(20, 2), default=0, info='总手续费')
    surplus_fee = db.Column(db.DECIMAL(20, 2), default=0, info='剩余手续费')

    bath = db.relationship('BuyDeal', backref='c_mall_buy_order', foreign_keys=[sell_batch_id])   # 售卖批次
    deal_priority = db.Column(db.Integer)  # 购买权限

    def buy_order_json(self):
        if self.update_time:
            self.update_time = self.update_time.strftime('%Y-%m-%d %H:%M:%S')
        return {
            'order_id': self.id,
            'create_time': self.create_time.strftime('%Y-%m-%d'),
            'update_time': self.update_time,
            'status': self.status,
            'num': self.num,
            'price': self.price,
            'trade_no': self.trade_no,
            'total_price': self.total_price,
            'surplus_num': self.surplus_num,
            'surplus_price': self.surplus_price,
            't_type': self.t_type,
        }


class BuyOrderGoods(db.Model):   # 购买商品订单
    __tablename__ = 'c_mall_buy_order_goods'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user_id = db.Column(db.String(32), index=True)
    order_id = db.Column(db.Integer, index=True)    # 购买订单id
    goods_id = db.Column(db.Integer, index=True)    # 商品id
    store_id = db.Column(db.Integer, index=True)    # 店铺id
    create_time = db.Column(db.DateTime, default=datetime.now)
    update_time = db.Column(db.DateTime)    # 提交退款时间
    end_time = db.Column(db.DateTime)    # 结束时间
    status = db.Column(db.Integer, default=0, index=True)   # 0默认 1退款申请 2审核通过 3失败
    back_status = db.Column(db.Integer, default=0, index=True)   # 0默认 1退款 2换货 3退款退货
    is_show = db.Column(db.Integer, default=0, index=True)  # 展示
    goods_status = db.Column(db.Integer, default=0, index=True)
    goods_name = db.Column(db.String(100))      # 商品名字
    goods_img = db.Column(db.String(255))      # 商品名字
    goods_num = db.Column(db.Integer, default=0)    # 商品数量
    price = db.Column(db.DECIMAL(20, 2))    # 商品价格
    integral = db.Column(db.Integer, default=0)    # 商品积分
    total_price = db.Column(db.DECIMAL(20, 2))      # 商品总价
    actual_price = db.Column(db.DECIMAL(20, 2))  # 实际支付
    freight = db.Column(db.Integer)  # 运费
    goods_code = db.Column(db.String(255))  # 商品编码
    actual_one = db.Column(db.DECIMAL(20, 2))  # 单个实付价格
    coupons_price = db.Column(db.DECIMAL(20, 2))  # 优惠价格
    refunds_price = db.Column(db.DECIMAL(20, 2), default=0)  # 退款价格
    refunds_num = db.Column(db.Integer, default=0)  # 退款数量

    logistics_name = db.Column(db.String(32))  # 物流名字
    logistics_num = db.Column(db.String(64))

    def buy_goods_json(self):
        return {
            'order_goods_id': self.id,
            'goods_id': self.goods_id,
            'order_id': self.order_id,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
            'status': self.status,
            'back_status': self.back_status,
            'goods_num': self.goods_num,
            'goods_name': self.goods_name,
            'goods_img': self.goods_img,
            'goods_status': self.goods_status,
            'price': self.price,
            'actual_price': self.actual_price,
            'actual_one': self.actual_one,
            'total_price': self.total_price,
            'goods_freight': self.freight,
            'logistics_name': self.logistics_name or '无',
            'logistics_num': self.logistics_num or '无',
            'refunds_num': self.refunds_num,
            'refunds_price': self.refunds_price,
        }


class Delivery(db.Model):   # 物流公司
    __tablename__ = 'c_mall_delivery'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    create_time = db.Column(db.DateTime, default=datetime.now)
    name = db.Column(db.String(50))
    simple = db.Column(db.String(50))


class Coupons(db.Model):    # 优惠券
    __tablename__ = 'c_mall_coupons'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    create_time = db.Column(db.DateTime, default=datetime.now)
    expiration_time = db.Column(db.Integer)  # 过期时间
    user_id = db.Column(db.String(32), index=True)
    goods_id = db.Column(db.Integer, index=True)
    sku_id = db.Column(db.Integer, index=True)
    store_id = db.Column(db.Integer, index=True)    # 商店ID
    status = db.Column(db.Integer, default=0, index=True)
    num = db.Column(db.Integer)
    name = db.Column(db.String(50))     # 名字
    price = db.Column(db.Integer)     # 金额
    minus_price = db.Column(db.Integer)     # 满减金额
    pay_price = db.Column(db.Integer)   # 支付价格
    coupons_img = db.Column(db.String(256))     # 图片
    user_level = db.Column(db.Integer)  # 等级
    discount = db.Column(db.DECIMAL(20, 2))     # 折扣

    def coupons_json(self):
        return {
            'coupons_id': self.id,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
            # 'num': self.num,
            'name': self.name,
            'price': self.price,
            'minus_price': self.minus_price,
            'pay_price': self.pay_price,
            'coupons_img': self.coupons_img,
            # 'discount': self.discount,
            'status': self.status,
        }


class HistoryPrice(db.Model):    # 历史价格
    __tablename__ = 'c_mall_history_price'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    create_time = db.Column(db.Date)
    goods_id = db.Column(db.Integer)
    status = db.Column(db.Integer, default=0)
    price = db.Column(db.DECIMAL(20, 2))
    y_price = db.Column(db.DECIMAL(20, 2))


class TransferOrder(db.Model):
    __tablename__ = 'c_mall_transfer_order'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    create_time = db.Column(db.DateTime, default=datetime.now)
    update_time = db.Column(db.DateTime)
    user_id = db.Column(db.String(100))     # 转方
    to_user_id = db.Column(db.String(100))     # 收方
    status = db.Column(db.Integer, default=0)     # 0待确认，1已确认，2用户手动撤单，3系统撤单
    t_type = db.Column(db.Integer)     # 1卖转买，2买转卖
    num = db.Column(db.Integer)     # 数量
    price = db.Column(db.DECIMAL(10, 2))     # 单价
    total_money = db.Column(db.DECIMAL(20, 2))     # 总金额
    goods_id = db.Column(db.Integer)
    batch_id = db.Column(db.Integer)

    def transfer_order_json(self):
        return {
            'transfer_order_id': self.id,
            'order_id': self.id,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
            'update_time': self.update_time.strftime('%Y-%m-%d %H:%M:%S') if self.update_time else '',
            't_type': self.t_type,
            'num': self.num,
            'price': self.price,
            'total_money': self.total_money,
            'status': self.status,
        }

